
package com.pvdnc.riru;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Method;
import java.util.Arrays;

public class RiruMainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("riru");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_riru_main);
        stringFromJNI();
        final EditText txtModuleDir=findViewById(R.id.txtModuleDir);
        txtModuleDir.setText(getApplicationInfo().nativeLibraryDir);
        Button cmdRiru=findViewById(R.id.cmdRiru);
        cmdRiru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //System.loadLibrary("riru_module_demo");
                String moduleDir=txtModuleDir.getText().toString();
                //initDispatcher();
                initDispatcher(moduleDir);
            }
        });
        Button cmdMagiskSu=findViewById(R.id.cmdMagiskSu);
        cmdMagiskSu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShellUtils.CommandResult cmdResult= ShellUtils.execCommand("id",true);
                Toast.makeText(RiruMainActivity.this, cmdResult.successMsg, Toast.LENGTH_SHORT).show();
            }
        });
        Button cmdReflector=findViewById(R.id.cmdReflector);
        cmdReflector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Reflector","attempt to find method");
                try{
                    @SuppressLint("PrivateApi")
                    Class<?> zygoteInitClass=Class.forName("com.android.internal.os.ZygoteInit");
                    Method[] methods= zygoteInitClass.getDeclaredMethods();
                    for (Method method:methods){
                        if(method.getName().equals("forkSystemServer")){
                            Log.d("Reflector","method params:"
                                    + Arrays.toString(method.getParameterTypes()));
                            Log.d("Reflector","found method:\n\t"+method);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    private native String stringFromJNI();

    public static native void initDispatcher(String moduleDir);
}
